<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工单标题" prop="applyTitle">
        <el-input
          v-model="queryParams.applyTitle"
          placeholder="请输入工单标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程名称" prop="businessProcesses">
        <el-select v-model="queryParams.businessProcesses" multiple placeholder="请选择">
          <el-option
            v-for="item in flowOptions"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="60" align="center" type="index" />
      <el-table-column label="流程名称" align="center" prop="templateName" />
      <el-table-column label="工单标题" align="center" prop="applyTitle" />
      <el-table-column label="工单号" align="center" prop="applyId" />
      <el-table-column label="申请人" align="center" prop="applicant.userName" />
      <el-table-column label="节点" align="center" prop="nodeName" />
      <el-table-column label="到达时间" align="center" prop="createTime" >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="doTask(scope.row.templateId, scope.row.tiid)">处理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup>
import { getReadList, getFlowInfo } from "@/api/biz/workOrder";

const { proxy } = getCurrentInstance();

const taskList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const flowOptions = ref([]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    applyTitle: null,
    templateName: null,
    businessProcesses: [],
  }
});

const { queryParams } = toRefs(data);

/** 查询待阅列表 */
async function getList() {
  loading.value = true;
  const response  = await getReadList(queryParams.value);
  taskList.value = response.rows;
  total.value = response.total;
  loading.value = false;
}

async function getFlowInfos() {
  const response = await getFlowInfo();
  flowOptions.value = response.data;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 打开任务处理页面 */
function doTask(templateId, tiid) {
  // router.push({ name: "taskDetail", params: { tiid } });
}
getFlowInfos();
getList();
</script>
